import util from '@/utils/test.js'
// 合并数组
const sumTopic = function(arr) {
  return arr.reduce((pre, cur) => pre.concat(cur.data), [])
}

// 知识点
const state = {
  module_title: '',
  topic_sum: 0, // 试卷的试题总数
  topic_rules: [], // 试题模型的数据
  topic_index: 0, // 试题的位置下标
  topic_id: 0, // 试题的id
  topic_correct_answer: [], // 试题的正确答案数组
  topic_correct_answer_key: [], // 试题的正确答案数组key
  topic_is_right: -1, // 试题正确错误 1 正确 0 错误
  topic_collected: 0
}

const Mutation = {
  SET_POINT_INIT: 'SET_POINT_INIT',
  SET_POINT_PREV: 'SET_POINT_PREV',
  SET_POINT_NEXT: 'SET_POINT_NEXT',
  SET_POINT_CURRENT: 'SET_POINT_CURRENT',
  SET_POINT_CURRENT_CHOOSE_ANSWER: 'SET_POINT_CURRENT_CHOOSE_ANSWER',
  SET_POINT_CURRENT_CORRECT_ANSWER: 'SET_POINT_CURRENT_CORRECT_ANSWER',
  SET_POINT_TOPIC_STATUS: 'SET_POINT_TOPIC_STATUS',
  SET_POINT_TOPIC_IS_RIGHT: 'SET_POINT_TOPIC_IS_RIGHT',
  SET_POINT_TOPIC_COLLECTED: 'SET_POINT_TOPIC_COLLECTED',
  SET_TOPIC_DEFAULT: 'SET_TOPIC_DEFAULT',
  SET_WRONG_TOPIC_ID: 'SET_WRONG_TOPIC_ID',
  SET_WRONG_TOPIC_IS_RIGHT: 'SET_WRONG_TOPIC_IS_RIGHT'
}
const mutations = {
  // 初始化知识点信息
  [Mutation.SET_POINT_INIT](state, data) {
    state.module_title = data.title
    state.topic_sum = data.sum
    state.topic_rules = data.rules
    // 添加课程类型
    state.topic_rules.forEach(topic => {
      topic.data.forEach(item => {
        item.question_type = topic.question_type
        if (util.isEmpty(item.answer_data)) {
          item.answer_data = []
        }
      })
    })
    let topicArr = sumTopic(state.topic_rules)
    if (!util.isEmpty(data.first_topic)) {
      let topicIndex = topicArr.findIndex(fruit => fruit.topic_id === data.first_topic)
      if (topicIndex < data.sum - 1) topicIndex += 1
      state.topic_index = topicIndex
    }
    state.topic_id = topicArr[state.topic_index].topic_id
  },
  // 试题上一题
  [Mutation.SET_POINT_PREV](state) {
    state.topic_index -= 1
    let topicArr = sumTopic(state.topic_rules)
    state.topic_id = topicArr[state.topic_index].topic_id
  },
  // 试题下一题
  [Mutation.SET_POINT_NEXT](state) {
    state.topic_index += 1
    if (!util.isEmpty(state.topic_rules)) {
      let topicArr = sumTopic(state.topic_rules)
      state.topic_id = topicArr[state.topic_index].topic_id
    }
  },
  // 切换到当前的试题
  [Mutation.SET_POINT_CURRENT](state, index) {
    state.topic_index = index
    let topicArr = sumTopic(state.topic_rules)
    state.topic_id = topicArr[state.topic_index].topic_id
  },
  // 设置当前的试题选择的答案
  [Mutation.SET_POINT_CURRENT_CHOOSE_ANSWER](state, data) {
    const rules = state.topic_rules
    const topic_id = state.topic_id
    rules.forEach(topic => {
      topic.data.forEach(item => {
        if (item.topic_id == topic_id) {
          item.answer_data = data.answer_data
        }
      })
    })
  },
  // 设置当前的试题状态为已答
  [Mutation.SET_POINT_TOPIC_STATUS](state) {
    const rules = state.topic_rules
    const topic_id = state.topic_id
    rules.forEach(topic => {
      topic.data.forEach(item => {
        if (item.topic_id == topic_id) {
          item.has_answered = 1
        }
      })
    })
  },
  // 设置当前正确的答案
  [Mutation.SET_POINT_CURRENT_CORRECT_ANSWER](state, data) {
    const topic = data.find(item => item.id == state.topic_id)
    let answer
    if (util.inArray(topic.question_type, [1, 2, 3, 4])) {
      answer = topic.options.filter(item => item.is_right == 1)
      state.topic_correct_answer = answer.map(item => item.id)
      state.topic_correct_answer_key = answer.map(item => item.key)
    } else if (topic.question_type == 7) {
      // 完型填空合拼正确的试题数组
      state.topic_correct_answer = topic.topics.map(ele => {
        let options = ele.options.find(item => item.is_right == 1)
        return {
          id: ele.id,
          answer: [options.id]
        }
      })
      answer = topic.topics.reduce(
        (pre, cur) => pre.concat(cur.options.filter(item => item.is_right == 1)),
        []
      )
      state.topic_correct_answer_key = answer.map(item => item.key)
    }
  },
  // 设置当前答案是否正确
  [Mutation.SET_POINT_TOPIC_IS_RIGHT](state) {
    state.topic_is_right = -1
    const topicRules = sumTopic(state.topic_rules)
    const topicId = state.topic_id
    const correctAnswer = state.topic_correct_answer
    let topicData = topicRules.find(item => item.topic_id == topicId)
    if (util.isEmpty(topicData.answer_data)) {
      state.topic_is_right = 0
      return
    }
    if (topicData.question_type == 7) {
      // 对比正确答案与选择的答案
      const equals = (a, b) =>
        a.length === b.length &&
        a.every((v, i) => {
          if (v.id == b[i].id) {
            return v.answer[0] == b[i].answer[0]
          }
        })
      state.topic_is_right = equals(correctAnswer, topicData.answer_data) ? 1 : 0
    } else {
      // 漏选
      let leak = correctAnswer.filter(v => topicData.answer_data.indexOf(v) === -1)
      // 错选
      let wrong = topicData.answer_data.filter(v => correctAnswer.indexOf(v) === -1)
      state.topic_is_right = util.isEmpty(leak) && util.isEmpty(wrong) ? 1 : 0
    }
  },
  // 设置收藏
  [Mutation.SET_POINT_TOPIC_COLLECTED](state, index) {
    state.topic_collected = index
  },
  // 销毁页面时初始化state默认状态
  [Mutation.SET_TOPIC_DEFAULT](state) {
    state.module_title = ''
    state.topic_sum = 0
    state.topic_rules = []
    state.topic_index = 0
    state.topic_id = 0
    state.topic_correct_answer = []
    state.topic_correct_answer_key = []
    state.topic_is_right = -1
    state.topic_collected = 0
  },
  /**
   * 错题练习
   */
  // 设置错误练习的id
  [Mutation.SET_WRONG_TOPIC_ID](state, id) {
    state.topic_id = id
  },
  // 设置当前答案是否正确
  [Mutation.SET_WRONG_TOPIC_IS_RIGHT](state, topicData) {
    state.topic_is_right = -1
    const correctAnswer = state.topic_correct_answer
    if (util.isEmpty(topicData.answer_data)) {
      state.topic_is_right = 0
      return
    }
    if (topicData.question_type == 7) {
      // 对比正确答案与选择的答案
      const equals = (a, b) =>
        a.length === b.length &&
        a.every((v, i) => {
          if (v.id == b[i].id) {
            return v.answer[0] == b[i].answer[0]
          }
        })
      state.topic_is_right = equals(correctAnswer, topicData.answer_data) ? 1 : 0
    } else {
      // 漏选
      let leak = correctAnswer.filter(v => topicData.answer_data.indexOf(v) === -1)
      // 错选
      let wrong = topicData.answer_data.filter(v => correctAnswer.indexOf(v) === -1)
      state.topic_is_right = util.isEmpty(leak) && util.isEmpty(wrong) ? 1 : 0
    }
  }
}

const Action = {
  SET_POINT_INIT: 'SET_POINT_INIT'
}
const actions = {
  // 初始化知识点信息
  [Action.SET_POINT_INIT]({ commit }, data) {
    commit(Mutation.SET_POINT_INIT, data)
  }
}

const getters = {
  topicAnswer: state => {
    const topicRules = sumTopic(state.topic_rules)
    const topicIndex = state.topic_index
    let topicData = topicRules.find((item, index) => index == topicIndex)
    return topicData
  },
  // 试题是否已做答
  topicAnswered: state => {
    const topicRules = sumTopic(state.topic_rules)
    const topicIndex = state.topic_index
    let topicData = topicRules.find((item, index) => index == topicIndex)
    return topicData.has_answered
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  Action,
  actions,
  getters
}
